﻿
Imports AutoMapper
Imports CookComputing.XmlRpc
Imports FTTK.Extensions
Imports FTTK.Entities
Imports System.Net
Imports Entities
Imports System.Globalization

Namespace Services.Implementations.XmlRpc
    <System.Runtime.InteropServices.GuidAttribute("A5BF66CE-DA20-4BC1-BFA7-EA14272AE984")> Friend NotInheritable Class PartnerServiceMapping
        Private Sub New()

        End Sub

        ''' <summary>
        ''' Defines the mapping for Automapper.
        ''' </summary>
        ''' <remarks></remarks>
        Friend Shared Sub DefineMapping()
            '' GetCategories
            Mapper.CreateMap(Of XmlRpcStruct, Entities.Category)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) CInt(a.Item("id")))) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) WebUtility.HtmlDecode(a.Item("name").ToString()))) _
                .ForMember(Function(a) a.NumberOfSubCategory, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer)("nb_sub_categories", -1)))

            Mapper.CreateMap(Of XmlRpcStruct, IEnumerable(Of Category))() _
                .ConvertUsing(Function(struct) struct.Keys.OfType(Of String).Select(Function(i) Mapper.Map(Of XmlRpcStruct, Entities.Category)(CType(struct.Item(i), XmlRpcStruct))))


            '' GetTags
            Mapper.CreateMap(Of XmlRpcStruct, Entities.Tag)() _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.Item("name").ToString())) _
                .ForMember(Function(a) a.Popularity, Sub(x) x.MapFrom(Function(a) CInt(a.Item("popularity"))))

            Mapper.CreateMap(Of XmlRpcStruct(), IEnumerable(Of Tag))() _
                .ConvertUsing(Function(structs) structs.Select(Function(x) Mapper.Map(Of XmlRpcStruct, Tag)(x)))

            '' GetGalleries
            Mapper.CreateMap(Of XmlRpcStruct, Entities.Gallery)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.Item("id").ToString())) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.Item("name").ToString())) _
                .ForMember(Function(a) a.NumberOfImages, Sub(x) x.MapFrom(Function(a) CInt(a.Item("nb_media")))) _
                .ForMember(Function(a) a.Type, Sub(x) x.ResolveUsing(Function(a)
                                                                         If a.ContainsKey("state") Then
                                                                             Select Case a.Item("state").ToString()
                                                                                 Case "0"
                                                                                     Return GalleryType.Lightbox
                                                                                 Case "1"
                                                                                     Return GalleryType.Folder
                                                                             End Select
                                                                         End If
                                                                         Return GalleryType.Unknown
                                                                     End Function)) _
                .ForMember(Function(a) a.Thumbnail, Sub(x) x.ResolveUsing(Function(a)
                                                                              Dim image As Entities.Image = Nothing
                                                                              If a.ContainsKey("thumbnail_url") Then
                                                                                  image = New Entities.Image() With {.Url = a.Item("thumbnail_url").ToString(),
                                                                                                                         .HtmlTag = a.Item("thumbnail_html_tag").ToString(),
                                                                                                                         .Height = CInt(a.Item("thumbnail_height")),
                                                                                                                         .Width = CInt(a.Item("thumbnail_width"))}
                                                                              End If
                                                                              Return image
                                                                          End Function))

            Mapper.CreateMap(Of XmlRpcStruct(), IEnumerable(Of Gallery))() _
                .ConvertUsing(Function(structs) structs.Select(Function(x) Mapper.Map(Of XmlRpcStruct, Gallery)(x)))

            ' '' GetColors
            Mapper.CreateMap(Of XmlRpcStruct, Entities.Color)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) CInt(a.Item("id")))) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.Item("name").ToString()))

            Mapper.CreateMap(Of XmlRpcStruct, IEnumerable(Of Color))() _
                .ConvertUsing(Function(struct) struct.Keys.OfType(Of String).Select(Function(i) Mapper.Map(Of XmlRpcStruct, Entities.Color)(CType(struct.Item(i), XmlRpcStruct))))


            '' GetCountries
            Mapper.CreateMap(Of XmlRpcStruct, Entities.Country)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) CInt(a.Item("id")))) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.Item("name").ToString()))


            Mapper.CreateMap(Of XmlRpcStruct(), IEnumerable(Of Country))() _
                .ConvertUsing(Function(structs) structs.Select(Function(x) Mapper.Map(Of XmlRpcStruct, Country)(x)))

            '' GetSearchResults (structures)
            Mapper.CreateMap(Of Entities.SearchRequest, FTTK.XmlRpc.SearchRequest)() _
                .ForMember(Function(a) a.Category1ID, Sub(x) x.MapFrom(Function(a) a.RepresentativeCategoryId)) _
                .ForMember(Function(a) a.Category2ID, Sub(x) x.MapFrom(Function(a) a.ConceptualCategoryId)) _
                .ForMember(Function(a) a.ColorName, Sub(x) x.MapFrom(Function(a) a.ColorName)) _
                .ForMember(Function(a) a.CountryID, Sub(x) x.MapFrom(Function(a) a.CountryId)) _
                .ForMember(Function(a) a.CreatorID, Sub(x) x.MapFrom(Function(a) a.CreatorId)) _
                .ForMember(Function(a) a.DetailLevel, Sub(x) x.MapFrom(Function(a) If(a.DetailLevel = SearchDetailLevel.Base, Nothing, 1))) _
                .ForMember(Function(a) a.GalleryID, Sub(x) x.MapFrom(Function(a) a.GalleryId)) _
                .ForMember(Function(a) a.LanguageID, Sub(x) x.MapFrom(Function(a) a.LanguageID)) _
                .ForMember(Function(a) a.Limit, Sub(x) x.MapFrom(Function(a) a.MaximumResult)) _
                .ForMember(Function(a) a.MediaID, Sub(x) x.MapFrom(Function(a) a.MediaId)) _
                .ForMember(Function(a) a.ModelID, Sub(x) x.MapFrom(Function(a) a.ModelId)) _
                .ForMember(Function(a) a.Offset, Sub(x) x.MapFrom(Function(a) a.Offset)) _
                .ForMember(Function(a) a.Order, Sub(x) x.MapFrom(Function(a) FTTK.XmlRpc.SearchResultsOrder.FromEntityOrder(a.ResultsOrderCriteria))) _
                .ForMember(Function(a) a.SerieID, Sub(x) x.MapFrom(Function(a) a.SerieId)) _
                .ForMember(Function(a) a.SimiliaID, Sub(x) x.MapFrom(Function(a) a.SimiliaId)) _
                .ForMember(Function(a) a.ThumbnailSize, Sub(x) x.MapFrom(Function(a) FTTK.XmlRpc.SearchThumbnailSize.FromEntitySize(a.ThumbnailSize))) _
                .ForMember(Function(a) a.Words, Sub(x) x.MapFrom(Function(a) a.Words)) _
                .ForMember(Function(a) a.Filters, Sub(x) x.MapFrom(Function(a) a.Filters))

            Mapper.CreateMap(Of Entities.SearchFilters, FTTK.XmlRpc.SearchFilters)() _
                .ForMember(Function(a) a.Age, Sub(x) x.MapFrom(Function(a) FTTK.XmlRpc.SearchAges.FromEntityAge(a.Age))) _
                .ForMember(Function(a) a.ContentTypeAll, Sub(x) x.MapFrom(Function(a) If(a.ContentType = SearchResultsContentType.All, 1, 0))) _
                .ForMember(Function(a) a.ContentTypeIllustration, Sub(x) x.MapFrom(Function(a) If(a.ContentType = SearchResultsContentType.Illustration And a.ContentType <> SearchResultsContentType.All, 1, 0))) _
                .ForMember(Function(a) a.ContentTypePhoto, Sub(x) x.MapFrom(Function(a) If(a.ContentType = SearchResultsContentType.Photo And a.ContentType <> SearchResultsContentType.All, 1, 0))) _
                .ForMember(Function(a) a.ContentTypeVector, Sub(x) x.MapFrom(Function(a) If(a.ContentType = SearchResultsContentType.Video And a.ContentType <> SearchResultsContentType.All, 1, 0)))


            '' GetSearchResults (results)
            Mapper.CreateMap(Of XmlRpcStruct(), IEnumerable(Of Media))() _
                .ConvertUsing(Function(structs) structs.Select(Function(x) Mapper.Map(Of XmlRpcStruct, Media)(x)))

            Mapper.CreateMap(Of XmlRpcStruct, Entities.Media)() _
                .ForMember(Function(a) a.Creator, Sub(x) x.ResolveUsing(Function(a)
                                                                            If a.ContainsKey("creator_id") Then
                                                                                If a.ContainsKey("creator_name") Then
                                                                                    Return New Entities.User() With {.Id = System.Convert.ToInt32(a.Item("creator_id")),
                                                                                                                     .Name = a.Item("creator_name").ToString()}
                                                                                Else
                                                                                    Return New Entities.User() With {.Id = System.Convert.ToInt32(a.Item("creator_id"))}
                                                                                End If
                                                                            Else
                                                                                Return Nothing
                                                                            End If
                                                                        End Function)) _
                .ForMember(Function(a) a.Downloads, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer?)("nb_downloads", Nothing))) _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer?)("id", Nothing))) _
                .ForMember(Function(a) a.Keywords, Sub(x) x.ResolveUsing(Function(a)
                                                                             If a.ContainsKey("keywords") Then
                                                                                 If TypeOf a.Item("keywords") Is XmlRpcStruct() Then
                                                                                     Dim keywordArray = CType(a.Item("keywords"), XmlRpcStruct())
                                                                                     Dim keywords = From b In keywordArray Select b.Item("name")
                                                                                     Return keywords.OfType(Of String)()
                                                                                 Else
                                                                                     Dim keywords = a.Item("keywords").ToString().Split(";"c)
                                                                                     Return keywords
                                                                                 End If
                                                                             Else
                                                                                 Return Nothing
                                                                             End If
                                                                         End Function)) _
                .ForMember(Function(a) a.Licenses, Sub(x) x.ResolveUsing(Function(a)
                                                                             If a.ContainsKey("licenses") Then
                                                                                 Dim structs = CType(a.Item("licenses"), Object())
                                                                                 Dim licenses = structs.Select(Function(b) Mapper.Map(Of XmlRpcStruct, License)(DirectCast(b, XmlRpcStruct))).ToList()
                                                                                 If a.ContainsKey("licenses_details") Then
                                                                                     Dim detailsStruct = CType(a.Item("licenses_details"), XmlRpcStruct)
                                                                                     For Each lic In licenses
                                                                                         If detailsStruct.ContainsKey(lic.Name) Then
                                                                                             Dim detail = Mapper.Map(Of XmlRpcStruct, LicenseDetail)(CType(detailsStruct.Item(lic.Name), XmlRpcStruct))
                                                                                             lic.Detail = detail
                                                                                         End If
                                                                                     Next
                                                                                 End If
                                                                                 Return licenses
                                                                             Else
                                                                                 Return Nothing
                                                                             End If
                                                                         End Function)) _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of String)("title", ""))) _
                .ForMember(Function(a) a.Thumbnail, Sub(x) x.ResolveUsing(Function(a)
                                                                              If a.ContainsKey("thumbnail_html_tag") Or a.ContainsKey("thumbnail_url") Then
                                                                                  Return New Entities.Image() With {.Height = a.GetValueOrDefault(Of Integer)("thumbnail_height", -1),
                                                                                                                    .Width = a.GetValueOrDefault(Of Integer)("thumbnail_width", -1),
                                                                                                                    .HtmlTag = a.GetValueOrDefault(Of String)("thumbnail_html_tag", Nothing),
                                                                                                                    .Url = a.GetValueOrDefault(Of String)("thumbnail_url", Nothing)}
                                                                              Else
                                                                                  Return Nothing
                                                                              End If
                                                                          End Function)) _
                .ForMember(Function(a) a.Thumbnail30, Sub(x) x.ResolveUsing(Function(a)
                                                                                If a.ContainsKey("thumbnail_30_html_tag") Or a.ContainsKey("thumbnail_30_url") Then
                                                                                    Return New Entities.Image() With {.Height = a.GetValueOrDefault(Of Integer)("thumbnail_30_height", -1),
                                                                                                                    .Width = a.GetValueOrDefault(Of Integer)("thumbnail_30_width", -1),
                                                                                                                    .HtmlTag = a.GetValueOrDefault(Of String)("thumbnail_30_html_tag", Nothing),
                                                                                                                    .Url = a.GetValueOrDefault(Of String)("thumbnail_30_url", Nothing)}
                                                                                Else
                                                                                    Return Nothing
                                                                                End If
                                                                            End Function)) _
                .ForMember(Function(a) a.Thumbnail110, Sub(x) x.ResolveUsing(Function(a)
                                                                                 If a.ContainsKey("thumbnail_110_html_tag") Or a.ContainsKey("thumbnail_110_url") Then
                                                                                     Return New Entities.Image() With {.Height = a.GetValueOrDefault(Of Integer)("thumbnail_110_height", -1),
                                                                                                                     .Width = a.GetValueOrDefault(Of Integer)("thumbnail_110_width", -1),
                                                                                                                     .HtmlTag = a.GetValueOrDefault(Of String)("thumbnail_110_html_tag", Nothing),
                                                                                                                     .Url = a.GetValueOrDefault(Of String)("thumbnail_110_url", Nothing)}
                                                                                 Else
                                                                                     Return Nothing
                                                                                 End If
                                                                             End Function)) _
                .ForMember(Function(a) a.Thumbnail400, Sub(x) x.ResolveUsing(Function(a)
                                                                                 If a.ContainsKey("thumbnail_400_html_tag") Or a.ContainsKey("thumbnail_400_url") Then
                                                                                     Return New Entities.Image() With {.Height = a.GetValueOrDefault(Of Integer)("thumbnail_400_height", -1),
                                                                                                                     .Width = a.GetValueOrDefault(Of Integer)("thumbnail_400_width", -1),
                                                                                                                     .HtmlTag = a.GetValueOrDefault(Of String)("thumbnail_400_html_tag", Nothing),
                                                                                                                     .Url = a.GetValueOrDefault(Of String)("thumbnail_400_url", Nothing)}
                                                                                 Else
                                                                                     Return Nothing
                                                                                 End If
                                                                             End Function)) _
                .ForMember(Function(a) a.MediaTypeId, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer?)("media_type_id", Nothing))) _
                .ForMember(Function(a) a.ConceptualCategory, Sub(x) x.ResolveUsing(Function(a)
                                                                                       If a.ContainsKey("cat2") Then
                                                                                           Return Mapper.Map(Of XmlRpcStruct, Entities.Category)(CType(a.Item("cat2"), XmlRpcStruct))
                                                                                       Else
                                                                                           Return Nothing
                                                                                       End If
                                                                                   End Function)) _
                .ForMember(Function(a) a.ConceptualSubCategories, Sub(x) x.ResolveUsing(Function(a)
                                                                                            If a.ContainsKey("cat2_hierarchy") Then
                                                                                                Dim hierarchy = CType(a.Item("cat2_hierarchy"), Object())
                                                                                                Return hierarchy.Select(Function(b) Mapper.Map(Of XmlRpcStruct, Entities.Category)(DirectCast(b, XmlRpcStruct)))
                                                                                            Else
                                                                                                Return Nothing
                                                                                            End If
                                                                                        End Function)) _
                .ForMember(Function(a) a.RepresentativeCategory, Sub(x) x.ResolveUsing(Function(a)
                                                                                           If a.ContainsKey("cat1") Then
                                                                                               Return Mapper.Map(Of XmlRpcStruct, Entities.Category)(CType(a.Item("cat1"), XmlRpcStruct))
                                                                                           Else
                                                                                               Return Nothing
                                                                                           End If
                                                                                       End Function)) _
                .ForMember(Function(a) a.RepresentativeSubCategories, Sub(x) x.ResolveUsing(Function(a)
                                                                                                If a.ContainsKey("cat1_hierarchy") Then
                                                                                                    Dim hierarchy = CType(a.Item("cat1_hierarchy"), Object())
                                                                                                    Return hierarchy.Select(Function(b) Mapper.Map(Of XmlRpcStruct, Entities.Category)(DirectCast(b, XmlRpcStruct)))
                                                                                                Else
                                                                                                    Return Nothing
                                                                                                End If
                                                                                            End Function)) _
                .ForMember(Function(a) a.Views, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer?)("nb_views", Nothing))) _
                .ForMember(Function(a) a.CreationDate, Sub(x) x.ResolveUsing(Function(a)
                                                                                 If a.ContainsKey("creation_date") Then
                                                                                     Return a.GetDateTime("creation_date", DateTime.MinValue)
                                                                                 Else
                                                                                     Return Nothing
                                                                                 End If
                                                                             End Function)) _
                .ForMember(Function(a) a.AffiliationLink, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of String)("affiliation_link", Nothing)))


            Mapper.CreateMap(Of XmlRpcStruct, Entities.License)() _
                .ForMember(Function(a) a.Name, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of String)("name", Nothing))) _
                .ForMember(Function(a) a.Price, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer)("price", -1)))

            Mapper.CreateMap(Of XmlRpcStruct, Entities.LicenseDetail)() _
                .ForMember(Function(a) a.DisplayAbbreviation, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of String)("display_abbr", Nothing))) _
                .ForMember(Function(a) a.Dpi, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer?)("dpi", Nothing))) _
                .ForMember(Function(a) a.Height, Sub(x) x.MapFrom(Function(a) a.GetNullableDouble("height", Nothing))) _
                .ForMember(Function(a) a.Width, Sub(x) x.MapFrom(Function(a) a.GetNullableDouble("width", Nothing))) _
                .ForMember(Function(a) a.Phrase, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of String)("phrase", Nothing))) _
                .ForMember(Function(a) a.Ratio, Sub(x) x.MapFrom(Function(a) a.GetNullableDouble("ratio", Nothing)))

            Mapper.CreateMap(Of XmlRpcStruct, Entities.Image)() _
                .ForMember(Function(a) a.Height, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer)("height", -1))) _
                .ForMember(Function(a) a.Width, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer)("width", -1))) _
                .ForMember(Function(a) a.Url, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of String)("url", Nothing)))

            Mapper.CreateMap(Of XmlRpcStruct, Entities.UserData)() _
                .ForMember(Function(a) a.Id, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Integer)("id", -1))) _
                .ForMember(Function(a) a.NumberOfCredits, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Double)("nb_credits", 0))) _
                .ForMember(Function(a) a.CreditValue, Sub(x) x.MapFrom(Function(a) a.GetValueOrDefault(Of Double)("credit_value", 0))) _
                .ForMember(Function(a) a.Language, Sub(x) x.ResolveUsing(Function(a)
                                                                             Return New Language() With {.Id = a.GetValueOrDefault(Of Integer)("language_id", 0), _
                                                                                                         .Name = a.GetValueOrDefault(Of String)("language_name", "")}
                                                                         End Function)) _
                .ForMember(Function(a) a.Currency, Sub(x) x.ResolveUsing(Function(a)
                                                                             Return New Currency() With {.Id = a.GetValueOrDefault(Of String)("currency_symbol", ""), _
                                                                                                         .Name = a.GetValueOrDefault(Of String)("currency_name", "")}
                                                                         End Function))

            Mapper.CreateMap(Of XmlRpcStruct, Entities.UserStats)() _
                .ForMember(Function(a) a.MediaAccepted, Sub(x) x.MapFrom(Function(a) CInt(a.Item("nb_media_accepted")))) _
                .ForMember(Function(a) a.MediaPurchased, Sub(x) x.MapFrom(Function(a) CInt(a.Item("nb_media_purchased")))) _
                .ForMember(Function(a) a.MediaSold, Sub(x) x.MapFrom(Function(a) CInt(a.Item("nb_media_sold")))) _
                .ForMember(Function(a) a.MediaUploaded, Sub(x) x.MapFrom(Function(a) CInt(a.Item("nb_media_uploaded")))) _
                .ForMember(Function(a) a.RankingAbsolute, Sub(x) x.MapFrom(Function(a) CInt(a.Item("ranking_absolute")))) _
                .ForMember(Function(a) a.RankingRelative, Sub(x) x.MapFrom(Function(a) CInt(a.Item("ranking_relative"))))


            Mapper.CreateMap(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStats)() _
                .ForMember(Function(a) a.MemberBoughtPhotos, Sub(x) x.ResolveUsing(Function(a)
                                                                                       Select Case a.Item2
                                                                                           Case UserStatsType.All
                                                                                               Return Mapper.Map(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData) _
                                                                                                   (New Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange) _
                                                                                                    (CType(a.Item1.Item("member_bought_photos"), XmlRpcStruct), a.Item2, a.Item3))
                                                                                           Case UserStatsType.MemberBoughtPhotos
                                                                                               Return Mapper.Map(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)(a)
                                                                                           Case Else
                                                                                               Return Nothing
                                                                                       End Select
                                                                                   End Function)) _
                .ForMember(Function(a) a.MemberDownloadedPhotos, Sub(x) x.ResolveUsing(Function(a)
                                                                                           Select Case a.Item2
                                                                                               Case UserStatsType.All
                                                                                                   Return Mapper.Map(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData) _
                                                                                                       (New Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange) _
                                                                                                       (CType(a.Item1.Item("member_downloaded_photos"), XmlRpcStruct), a.Item2, a.Item3))
                                                                                               Case UserStatsType.MemberDownloadedPhotos
                                                                                                   Return Mapper.Map(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)(a)
                                                                                               Case Else
                                                                                                   Return Nothing
                                                                                           End Select
                                                                                       End Function)) _
                .ForMember(Function(a) a.MemberEarnedCredits, Sub(x) x.ResolveUsing(Function(a)
                                                                                        Select Case a.Item2
                                                                                            Case UserStatsType.All
                                                                                                Return Mapper.Map(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData) _
                                                                                                    (New Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange) _
                                                                                                    (CType(a.Item1.Item("member_earned_credits"), XmlRpcStruct), a.Item2, a.Item3))
                                                                                            Case UserStatsType.MemberEarnedCredits
                                                                                                Return Mapper.Map(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)(a)
                                                                                            Case Else
                                                                                                Return Nothing
                                                                                        End Select
                                                                                    End Function)) _
                .ForMember(Function(a) a.MemberViewedPhotos, Sub(x) x.ResolveUsing(Function(a)
                                                                                       Select Case a.Item2
                                                                                           Case UserStatsType.All
                                                                                               Return Mapper.Map(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData) _
                                                                                                   (New Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange) _
                                                                                                   (CType(a.Item1.Item("member_viewed_photos"), XmlRpcStruct), a.Item2, a.Item3))
                                                                                           Case UserStatsType.MemberViewedPhotos
                                                                                               Return Mapper.Map(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)(a)
                                                                                           Case Else
                                                                                               Return Nothing
                                                                                       End Select
                                                                                   End Function))


            Mapper.CreateMap(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), Entities.UserAdvancedStatsData)() _
                .ForMember(Function(a) a.ToBeGenerated, Sub(x) x.ResolveUsing(Function(a)
                                                                                  If a.Item1.ContainsKey("to_be_generated") Then
                                                                                      Return a.Item1.GetValueOrDefault(Of Boolean)("to_be_generated", False)
                                                                                  Else
                                                                                      Return False
                                                                                  End If
                                                                              End Function)) _
                .ForMember(Function(a) a.Details, Sub(x) x.MapFrom(Function(a) Mapper.Map(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), IEnumerable(Of Entities.UserAdvancedStatsDetail))(a)))

            Mapper.CreateMap(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), IEnumerable(Of Entities.UserAdvancedStatsDetail))() _
                .ConvertUsing(Of XmlStructToEnumerableDetailsConverter)()


        End Sub

        Private Class XmlStructToEnumerableDetailsConverter
            Implements ITypeConverter(Of Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), IEnumerable(Of Entities.UserAdvancedStatsDetail))

            Public Function Convert(context As AutoMapper.ResolutionContext) As System.Collections.Generic.IEnumerable(Of Entities.UserAdvancedStatsDetail) Implements AutoMapper.ITypeConverter(Of System.Tuple(Of CookComputing.XmlRpc.XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange), System.Collections.Generic.IEnumerable(Of Entities.UserAdvancedStatsDetail)).Convert
                Dim sourceTuple = CType(context.SourceValue, Tuple(Of XmlRpcStruct, Entities.UserStatsType, Entities.UserStatsTimeRange))
                Dim list = New List(Of Entities.UserAdvancedStatsDetail)()
                For Each key In sourceTuple.Item1.Keys.OfType(Of String)()
                    Dim detail As UserAdvancedStatsDetail = Nothing
                    Select Case sourceTuple.Item3
                        Case UserStatsTimeRange.Day
                            Dim datepart = key.Split("-"c)
                            If datepart.Count > 2 Then
                                Try
                                    detail = New UserAdvancedStatsDayDetail()
                                    CType(detail, UserAdvancedStatsDayDetail).Day = CInt(datepart(2))
                                    CType(detail, UserAdvancedStatsDayDetail).Month = CInt(datepart(1))
                                    detail.Year = CInt(datepart(0))
                                Catch ex As Exception
                                    detail = Nothing
                                End Try
                            End If
                        Case UserStatsTimeRange.Month
                            Dim datepart = key.Split(" "c)
                            If datepart.Count > 1 Then
                                Try
                                    detail = New UserAdvancedStatsMonthDetail()
                                    CType(detail, UserAdvancedStatsMonthDetail).Month = datepart(0)
                                    detail.Year = CInt(datepart(1))
                                Catch ex As Exception
                                    detail = Nothing
                                End Try
                            End If
                        Case UserStatsTimeRange.Quarter
                            Dim datepart = key.Split("-"c)
                            If datepart.Count > 1 Then
                                Try
                                    detail = New UserAdvancedStatsQuarterDetail()
                                    CType(detail, UserAdvancedStatsQuarterDetail).Quarter = CInt(datepart(1))
                                    detail.Year = CInt(datepart(0))
                                Catch ex As Exception
                                    detail = Nothing
                                End Try
                            End If
                        Case UserStatsTimeRange.Week
                            Dim datepart = key.Split("-"c)
                            If datepart.Count > 1 Then
                                Try
                                    detail = New UserAdvancedStatsWeekDetail()
                                    CType(detail, UserAdvancedStatsWeekDetail).Week = CInt(datepart(1))
                                    detail.Year = CInt(datepart(0))
                                Catch ex As Exception
                                    detail = Nothing
                                End Try
                            End If
                        Case UserStatsTimeRange.Year
                            Try
                                detail = New UserAdvancedStatsYearDetail()
                                detail.Year = CInt(key)
                            Catch ex As Exception
                                detail = Nothing
                            End Try
                    End Select
                    If detail IsNot Nothing Then
                        detail.Value = sourceTuple.Item1.GetDouble(key, 0, New CultureInfo("en"))
                        list.Add(detail)
                    End If
                Next
                Return list
            End Function
        End Class
    End Class

    
End Namespace
